﻿using System.Collections;
using System.Collections.Generic;
using AutoChess;
using UnityEngine;
using UnityEngine.UI;
using Yunchang;

class ShopHeroItem : MonoBehaviour
{
    public Transform buffRoot, effectRoot;
    public GroupSpriteKit buffObj;

    public GameObject upStarBtn, content;

    public Text heroName;
    public Text goldNum;
    public Text goldNumRed;

    private CommonEffect quanlityEffect, heroTipEffect;

    private void OnDestroy()
    {
        if (quanlityEffect != null)
            quanlityEffect.Clear();
        if (heroTipEffect != null)
            heroTipEffect.Clear();
    }
   

    public void Refresh(ACHero hero)
    {
        if (hero == null)
        {
            content.SetActive(false);
            return;
        }
        else
        {
            //更新英雄品质
            var qualityEffectId = UtilityHelper.GetEffectIdByHeroRare(hero.ChessHeroTalbe.rare);
            if (quanlityEffect == null)
            {
                quanlityEffect = UtilityHelper.PlayEffect(qualityEffectId, effectRoot);
            }
            else if (quanlityEffect.GetResId() != qualityEffectId)
            {
                quanlityEffect.Clear();
                quanlityEffect = null;
                quanlityEffect = UtilityHelper.PlayEffect(qualityEffectId, effectRoot);
            }

            var heroEffectId = UtilityHelper.GetEffectIdByHeroRare(hero.Attr.GetAtrribute<HeroEffectType>(ActorActionAttrDef.Hero_GettedNum_Effect));
            //更新数量特效
            if (hero.Attr.GetAtrribute<HeroEffectType>(ActorActionAttrDef.Hero_GettedNum_Effect) == HeroEffectType.None)
            {
                if(heroTipEffect!=null)
                    heroTipEffect.Clear();
                heroTipEffect = null;
            }
            else 
            {
                if (heroTipEffect == null)
                {
                    heroTipEffect = UtilityHelper.PlayEffect(heroEffectId, effectRoot);
                }
                else if (heroTipEffect != null && heroTipEffect.GetResId() != heroEffectId)
                {
                    heroTipEffect.Clear();
                    heroTipEffect = UtilityHelper.PlayEffect(heroEffectId, effectRoot);
                }
            }

            content.SetActive(true);
        }
        var chessTable = Table.Blo.AutoChessBlo.GetChessTable(hero.ChessID);
        var chessHero = Table.Blo.AutoChessBlo.GetChessHero(chessTable.heroId);
        var heroTable = Table.Blo.TableHeroBlo.GetTableHero(chessTable.heroId);
        var starNum = chessTable.star;

        ShowHeroNameAndGold(hero, heroTable, chessTable);
        RefreshGroupIcon(Table.Blo.AutoChessBlo.ParseGroups(chessHero.acHeroGroups));
    }

    //刷新阵营buff图标
    private void RefreshGroupIcon(List<int> groups)
    {
        buffRoot.DestroyChildren();
        for (int i = 0; i < groups.Count; i++)
        {
            var groupTable = Table.Blo.AutoChessBlo.GetGroup(groups[i]);
            var icon = Instantiate<GroupSpriteKit>(buffObj, buffRoot);
            icon.gameObject.SetActive(true);
            if (groupTable != null)
            {
                icon.SetSpriteName(groupTable.altasName);
            }
            else
            {
                Debug.LogError("Group表数据错误 不存在：" + groups[i]);
            }
        }
    }
    private void ShowHeroNameAndGold(ACHero hero, Table.DataModel.TableHero heroTable, Table.DataModel.TableAutoChessHeroChess chessTable)
    {
        if (hero.MapGrid.GridType == GridType.ShopGrid)
        {
            heroName.text = heroTable.heroName;

            goldNum.gameObject.SetActive(AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= chessTable.buyPrice);
            goldNumRed.gameObject.SetActive(AutoChessManager.Ins.cacheData.CurPlayerData.Gold < chessTable.buyPrice);
            var text = AutoChessManager.Ins.cacheData.CurPlayerData.Gold >= chessTable.buyPrice ? goldNum : goldNumRed;
            text.text = "x " + chessTable.buyPrice;
        }
        else
        {
            heroName.text = "";
            goldNum.gameObject.SetActive(true);
            goldNumRed.gameObject.SetActive(false);
            goldNum.text = "";
        }
    }
}
